package com.hwd.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.hwd.srb.common.constant.Constants;
import com.hwd.srb.common.exception.Assert;
import com.hwd.srb.common.result.R;
import com.hwd.srb.common.result.ResponseEnum;
import com.hwd.srb.core.hfb.RequestHelper;
import com.hwd.srb.core.pojo.entity.UserAccount;
import com.hwd.srb.core.service.TransFlowService;
import com.hwd.srb.core.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/api/core/userAccount")
@Api(tags = "会员账户接口")
@Slf4j
public class ApiUserAccountController {

    @Resource
    private UserAccountService userAccountService;

    @Resource
    private TransFlowService transFlowService;

    /**
     * 充值成功异步回调
     *
     * @param request 请求对象
     * @return java.lang.String
     */
    @ApiParam(value = "充值成功异步回调")
    @PostMapping("/notify")
    public String notifyReCharge(HttpServletRequest request) {

        // 获取结果参数
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户充值异步回调：" + JSON.toJSONString(paramMap));

        // 校验签名
        if (!RequestHelper.isSignEquals(paramMap)) {
            log.info("用户充值异步回调签名错误：" + JSON.toJSONString(paramMap));
            return "fail";
        }

        // 校验充值交易是否成功
        if (!"0001".equals(paramMap.get("resultCode"))) {
            log.info("用户充值异步回调充值失败：" + JSON.toJSONString(paramMap));
            return "fail";
        }

        String agentBillNo = (String) paramMap.get("agentBillNo"); //商户充值订单号

        // 幂等性校验
        boolean hasSave = transFlowService.isSaveTransFlow(agentBillNo);
        Assert.isTrue(!hasSave, ResponseEnum.RECHARGE_CHONGFU);

        // 调用业务层方法，
        userAccountService.notifyReCharge(paramMap);

        return "success";
    }

    /**
     * 会员账号充值，返回汇付宝表单字符串
     *
     * @param chargeAmt 充值金额
     * @param request   请求对象
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation(value = "充值")
    @PostMapping("/auth/commitCharge/{chargeAmt}")
    public R commitCharge(@ApiParam(value = "充值金额", required = true)
                          @PathVariable("chargeAmt") BigDecimal chargeAmt,
                          HttpServletRequest request) {
        // 在请求头中回去userId
        Long userId = Long.valueOf(request.getHeader(Constants.USER_ID));

        String formStr = userAccountService.commitCharge(chargeAmt, userId);

        return R.ok().data("formStr", formStr);
    }

    /**
     * 根据userId获取账户余额
     *
     * @param request 请求对象
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation(value = "获取账户余额")
    @GetMapping("/auth/getAccount")
    public R getAccount(HttpServletRequest request) {

        Long userId = Long.valueOf(request.getHeader(Constants.USER_ID));

        BigDecimal account = userAccountService.getAccountByUserId(userId);

        return R.ok().data("account", account);
    }

    /**
     * 获取账户信息
     *
     * @param request 请求对象
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation(value = "获取账户信息")
    @GetMapping("/getUserAccount")
    public R getUserAccount(HttpServletRequest request) {

        Long userId = Long.valueOf(request.getHeader(Constants.USER_ID));

        UserAccount userAccount = userAccountService.getUserAccountByUserId(userId);

        return R.ok().data("userAccount", userAccount);
    }
}

